package com.zzw.controller;

import com.zzw.entity.ProductType;
import com.zzw.entity.Result;
import com.zzw.service.ProductTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Project: warehouse
 * @Description:
 * @Author: zzw
 */
@RestController
@RequestMapping("/productType")
@Slf4j
@Api(tags = "商品分类模块")
public class ProductTypeController {

    @Autowired
    private ProductTypeService productTypeService;

    /**
     * 查询商品分类树的url接口
     *
     * @return
     */
    @GetMapping("/tree")
    @ApiOperation("查询商品分类树")
    public Result productTypeTree() {
        List<ProductType> productTypeList = productTypeService.allProductTypeTree();
        return Result.ok(productTypeList);
    }

    /**
     * 校验分类编码是否已存在的url接口
     *
     * @param typeCode
     * @return
     */
    @GetMapping("/code/{typeCode}")
    @ApiOperation("校验商品分类编码")
    public Result checkTypeCode(@PathVariable String typeCode) {
        return productTypeService.queryTypeCode(typeCode);
    }

    /**
     * 添加商品分类的url接口
     *
     * @param productType
     * @return
     */
    @PostMapping
    @ApiOperation("添加商品分类")
    public Result saveProductType(@RequestBody ProductType productType) {

        //需要做校验，商品分类不存在再添加，否则会报500错误
        return productTypeService.saveProductType(productType);
    }

    /**
     * 删除商品分类的url接口
     *
     * @param typeId
     * @return
     */
    @DeleteMapping("/{typeId}")
    @ApiOperation("删除商品分类")
    public Result deleteProductType(@PathVariable Integer typeId) {
        return productTypeService.removeProductType(typeId);
    }

    /**
     * 修改商品分类的url接口
     *
     * @param productType
     * @return
     */
    @PutMapping
    @ApiOperation("修改商品分类")
    public Result updateProductType(@RequestBody ProductType productType) {
        return productTypeService.updateProductType(productType);
    }
}
